package vip.huhaiong.hlfood.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import vip.huhaiong.hlfood.pojo.Rs;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    public RedisUtil(StringRedisTemplate stringRedisTemplate){
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public void set(String key, Rs object){
        String value = JSONObject.toJSONString(object);
        stringRedisTemplate.opsForValue().set(key,value,300, TimeUnit.SECONDS);
    }
    public Rs get(String key){
        String value = stringRedisTemplate.opsForValue().get(key);
        return JSONObject.parseObject(value,Rs.class);
    }
}
